<?php
class Rfq_model extends Model {

	function rfq_model ()
	{
		parent::Model();
			$this->obj =& get_instance();
	}

	function getUserRFQs($user_id)
	{
	
	}
	
	function countAllRFQs()
	{
	
	}

	function countUserRFQs($user_id)
	{

	}

	function addRFQ($rfqData)
	{
		if ($this->db->insert('rfq', $rfqData)) {
			return $this->db->insert_id();
		} else {
			return FALSE;
		}		
	}

	function addRFQItem($rfqItems)
	{
		if ($this->db->insert('rfq_item', $rfqItems)) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function updateRFQ($rfqData, $rfq_id)
	{
		$this->db->where('id', $rfq_id);

		if ($this->db->update('rfq', $rfqData)) {
			return $rfq_id;
		} else {
			return FALSE;
		}
	}

	function deleteRFQ($rfq_id)
	{
		$this->db->where('rfq_id', $rfq_id);
		$this->db->delete('rfq_response'); // remove rfq response info

		$this->delete_RFQ_items($rfq_id); // remove rfq items

		$this->db->where('id', $rfq_id);
		$this->db->delete('rfq'); // remove rfq info
	}

	function delete_RFQ_items($rfq_id)
	{
		$this->db->where('rfq_id', $rfq_id);
		$this->db->delete('rfq_item');
	}

	function getSingleRFQ($rfq_id)
	{
		//Need to create select statement to grab single RFQ
		$this->db->select('rfq.*, vendors.*, rfq_items.*');
		$this->db->join('vendors', 'rfq.vendor_id = vendors.id');
		$this->db->join('rfq_item', 'rfq.id = rfq_item.rfq_id');
		$this->db->groupby('rfq.id'); 
		$this->db->where('rfq.id', $rfq_id);
		return $this->db->get('rfq');
	}

	function getRFQitems($rfq_id)
	{
		$this->db->where('rfq_id', $rfq_id);
		$items = $this->db->get('rfq_item');
		if ($items->num_rows() > 0) {
			return $items;
		} else {
			return FALSE;
		}
	}

	function getRFQHistory($rfq_id)
	{

	}

	function getRFQs($offset=0, $limit=100, $status, $days_response_due=2)
	{
		//create select statement to grab all RFQs
		// just let me say that http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html sucks...
		if ($status == 'overdue') {
			$having = "daysOverdue <= -$days_payment_due AND (ROUND(amount_paid, 2) < ROUND(subtotal, 2) OR amount_paid is null)";
		} elseif ($status == 'open') {
			$having = '(ROUND(amount_paid, 2) < ROUND(subtotal, 2) or amount_paid is null)';
		} elseif ($status == 'closed') {
			$having = 'ROUND(amount_paid, 2) >= ROUND(subtotal, 2)';
		} else {
			$having = 'invoices.id'; // just list everything
		}
		
		$this->db->select('invoices.*, clients.name');
		$this->db->select('(SELECT SUM(amount_paid) FROM invoice_payments WHERE invoice_id=invoices.id) AS amount_paid');
		$this->db->select('TO_DAYS(invoices.dateIssued) - TO_DAYS(curdate()) AS daysOverdue');
		$this->db->select('(SELECT SUM(invoice_items.amount * invoice_items.quantity + (invoice_items.amount * invoice_items.quantity * (invoices.tax1_rate/100 + invoices.tax2_rate/100) * invoice_items.taxable)) FROM invoice_items WHERE invoice_items.invoice_id=invoices.id) AS subtotal');

		$this->db->join('clients', 'invoices.client_id = clients.id');
		$this->db->join('invoice_items', 'invoices.id = invoice_items.invoice_id');
		$this->db->join('invoice_payments', 'invoices.id = invoice_payments.invoice_id', 'left');
		$this->db->having($having);
		$this->db->orderby('dateIssued desc, rfqNumber desc');
		$this->db->groupby('rfq.id'); 
		$this->db->offset($offset);
		$this->db->limit($limit);
		return $this->db->get('rfq');
	}

	function lastRFQNumber()
	{
		$this->db->where('rfqNumber !=', '');
		$this->db->orderby("id", "desc"); 
		$this->db->limit(1);
		$query = $this->db->get('rfq');
				
		if ($query->num_rows() > 0) {
			return $query->row()->rfqNumber;
		} else {
			return '0';
		}
	}

	function uniqueRFQNumberEdit($rfqNumber, $rfq_id)
	{
		$this->db->where('rfqNumber',$rfqNumber);
		$this->db->where('id!=',$rfq_id);
		$query = $this->db->get('rfq');
		$num_rows = $query->num_rows();
		if ($num_rows == 0) {
//			$row = $query->result_array();
			return TRUE;
		} else {
			return FALSE;
		}
	}




}


?>
